package leetcode_core.leetcode_2;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Permute {

    private List<List<Integer>> ans = new ArrayList<>();
    private boolean[] book = new boolean[10];

    public List<List<Integer>> permute(int[] nums) {
        Arrays.sort(nums);
        backtrack(nums,new LinkedList<>());
        return ans;
    }

    private void backtrack(int[] nums, LinkedList<Integer> track){
        if(track.size() == nums.length){
            ans.add(new ArrayList<>(track));
        }
        for(int i = 0;i<nums.length;i++){
            if(book[i]){
                continue;
            }
            //做出选择
            track.add(nums[i]);
            book[i] = true;
            //低估回溯
            backtrack(nums,track);
            //撤销选择
            track.removeLast();
            book[i] = false;
        }
    }



}
